﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Sunny.Entity;
using Sunny.Model.RequestParam;
using Sunny.Model.Vo;
using Sunny.Service;
using Sunny.Util.Attributes;
using Sunny.Util.Enums;

namespace Sunny.Api.Controllers
{
    [ApiController]
    [Route("/system/dept")]
    public class SysDeptController
    {
        private readonly DeptService _deptService;

        public SysDeptController(DeptService deptService)
        {
            _deptService = deptService;
        }
        
        #region 获取部门下拉列表树

        [HttpGet("treeselect")]
        public async Task<Result> TreeSelect()
        {
             List<DeptTreeVo> tree =  await _deptService.GetDeptTree();
            return Result.Success(tree);
        }
        #endregion

        #region 获取用户部门树

        [HttpGet("roleDeptTreeselect/{roleId}")]
        public async Task<Result> RoleDeptTreeSelect([FromRoute]long roleId)
        {
            RoleDeptTreeSelectVo roleDeptTreeSelectVo = await _deptService.RoleDeptTreeSelect(roleId);
            return Result.Success(roleDeptTreeSelectVo);
        }

        #endregion

        #region 获取部门列表
        [HasPermission("system:dept:list")]
        [HttpGet("list")]
        public async Task<Result> List([FromQuery]DeptListParam param)
        {
            List<SysDept> sysDepts = await _deptService.List(param);
            return Result.Success(sysDepts);
        }
        #endregion

        #region 新增部门
        [HasPermission("system:dept:add")]
        [LogInfo(Title = "部门管理",BusinessType = BusinessType.Insert)]
        [HttpPost]
        public async Task<Result> Add([FromBody]SysDept sysDept)
        {
            await _deptService.Add(sysDept);
            return Result.Success();
        }

        #endregion

        #region 获取部门详情
        [HasPermission("system:dept:query")]
        [HttpGet("{deptId}")]
        public async Task<Result> Info([FromRoute]long deptId)
        {
            SysDept sysDept = await _deptService.GetById(deptId);
            return Result.Success(sysDept);
        }
        #endregion
        
        #region 修改部门
        [HasPermission("system:dept:edit")]
        [LogInfo(Title = "部门管理",BusinessType = BusinessType.Update)]
        [HttpPut]
        public async Task<Result> Edit([FromBody]SysDept sysDept)
        {
            await _deptService.Update(sysDept);
            return Result.Success();
        }

        #endregion

        #region 删除部门
        [HasPermission("system:dept:remove")]
        [LogInfo(Title = "部门管理",BusinessType = BusinessType.Delete)]
        [HttpDelete("{deptId}")]
        public async Task<Result> Delete([FromRoute]long deptId)
        {
            await _deptService.Delete(deptId);
            return Result.Success();
        }
        #endregion
    }
}